//
//  HMVisitorView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/2.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SnapKit


/// 协议
// 在Swift要使用weak修饰的代理,那么其遵守的协议需要继承于基协议 NSObjectProtocol
// 协议的可选方法的两个关键字: @objc & optional
@objc protocol HMVisitorViewDelegate: NSObjectProtocol{
    // 登录按钮点击
    optional func visitorView(visitorView: HMVisitorView, loginButtonDidCliked: UIButton);
}




class HMVisitorView: UIView {
    
    // 代理
    weak var delegate : HMVisitorViewDelegate?
    
    var loginCallBack: (()->())?
    
    // 因为是自定义View，先把构造方法写上
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("访客视图不允许xib调用")
    }
    
    // 构造方法创建视图
    func setNameAndTitle(imageName:String,title:String,isHome:Bool) {
        

        descLabel.text = title
        
        if isHome {
            // 如果是home就开启动画
            startAnmiation()
        } else {
            
            circleView.hidden = true
            iconView.image = UIImage(named: imageName)
        }
    }
    
    // 自定义View
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // 加载控件
        addSubview(circleView)
        addSubview(shadowView)
        addSubview(iconView)
        addSubview(descLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        circleView.translatesAutoresizingMaskIntoConstraints = false
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        descLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        /*
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 6))
        
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 6))
        addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: descLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: descLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: descLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: descLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
         */
        
        // SnipKit
        
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        circleView.snp_makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        descLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(descLabel)
            make.top.equalTo(descLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(descLabel)
            make.top.equalTo(descLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        shadowView.snp_makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
    }
    
    // 开启动画
    func startAnmiation() {
        
        // 开启旋转动画
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        anmi.duration = 20
        anmi.toValue = 2 * M_PI
        anmi.repeatCount = MAXFLOAT
        // 切换页面动画停止
        anmi.removedOnCompletion = false
        circleView.layer.addAnimation(anmi, forKey: nil)

    }
    
    // MARK: 按钮单击事件
    func loginButtonClick(button:UIButton) {
        loginCallBack?() 
    }
    

    // MARK: 懒加载视图控件
    // 1.中央图标
    private lazy var iconView :UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    // 2.中央转圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 3.下方文字  文字都要设置字体，颜色，居中方式，需要写一个分类
    private lazy var descLabel: UILabel =  {
       let u = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), textAlignment: .Center)
        u.numberOfLines = 0
        u.font = UIFont.systemFontOfSize(14)
        return u
    }()
    // 4.注册
    private lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        // 添加点击事件
        button.addTarget(self, action: #selector(HMVisitorView.loginButtonClick(_:)), forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    // 5.登录
    private lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 添加点击事件
        button.addTarget(self, action: #selector(HMVisitorView.loginButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    // 6.阴影背景
    private lazy var shadowView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
